//
//  WEXWeiboCell.swift
//  microBlog
//
//  Created by apple on 16/4/20.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit
import SnapKit

// MARK: - 常量设置
// 用户昵称字体大小
let userNameFont: CGFloat = 14
// 发送时间字体大小
let sendTimeFont: CGFloat = 13
// 来源字体大小
let sourceFont: CGFloat = 13
// 公共间距
let commonMargin: CGFloat = 10
// 正文字体大小
let contentFont: CGFloat = 13
// toolBar按钮的文字字体大小
let toolBarButtonFont: CGFloat = 14
// toolBar的高度
let toolBarRowHeight: CGFloat = 35



class WEXWeiboCell: UITableViewCell {
    
    var toolBarCon: Constraint?
    
//    传递数据
    var statusViewMdoel: WEXStatusViewModel? {
        didSet{
            
            originialStatus.statusViewModel = statusViewMdoel
            
            retweeteStatus.statusViewModel = statusViewMdoel
            
            // 卸载原有的约束
            toolBarCon?.uninstall()
            
            // 判断是否有转发内容
            if statusViewMdoel?.status?.retweeted_status != nil {
            
//              显示转发内容
                retweeteStatus.hidden = false
                
//                更新约束
                StatusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                  toolBarCon = make.top.equalTo(retweeteStatus.snp_bottom).constraint
                    
                    
                })
                
            }else {
                
//                隐藏转发内容
                retweeteStatus.hidden = true
                
//                更新约束
                StatusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    toolBarCon = make.top.equalTo(originialStatus.snp_bottom).constraint
                    
                })
            }
        }
        
    }

//    重写init方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func setupUI() {
       
        // 添加原创微博内容
        contentView.addSubview(originialStatus)
        // 添加底部toolBar
        contentView.addSubview(StatusToolBar)
        // 添加转发内容
        contentView.addSubview(retweeteStatus)
        
        // 设置背景颜色
        backgroundColor = UIColor(white: 245 / 255.0 , alpha: 1)
        // 当前cell 的点击样式
        selectionStyle = .None
        
        // 设置原创微博的约束
        originialStatus.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(5)
            make.leading.trailing.equalTo(contentView)
//            make.height.equalTo(80)
        }
        
        // 设置底部toolBar的约束
        StatusToolBar.snp_makeConstraints { (make) -> Void in
            
           toolBarCon =  make.top.equalTo(retweeteStatus.snp_bottom).constraint
            make.leading.trailing.equalTo(originialStatus)
            make.height.equalTo(toolBarRowHeight)
        }
        
        // 设置转发微博的内容的约束
        retweeteStatus.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originialStatus.snp_bottom)
            make.leading.trailing.equalTo(originialStatus)
//            make.height.equalTo(60)
            
        }
        
        // 设置contentView的约束
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(StatusToolBar.snp_bottom)
            make.leading.trailing.bottom.equalTo(self)
        }
        
        
    }
    
    // MARK: - 懒加载控件
    
    // 原创view
    private lazy var originialStatus: WEXOriginalStatusView = WEXOriginalStatusView()
    
    // toolBar
    private lazy var StatusToolBar: WEXStatusToolBar = WEXStatusToolBar()
    
    // 转发view
    private lazy var retweeteStatus: WEXRetweetStatusView = WEXRetweetStatusView()
    
}
